#import "SEGAnalyseBehaviorManager.h"

#if __has_include(<Commom/SEGModel.h>)
#import <Commom/SEGModel.h>
#else
#import "SEGModel.h"
#endif

#if __has_include(<SEGPlatform/SEGPlatformManager.h>)
#import <SEGPlatform/SEGPlatformManager.h>
#else
#import "SEGPlatformManager.h"
#endif

#if __has_include(<Commom/SEGHTTPBusinessManager.h>)
#import <Commom/SEGHTTPBusinessManager.h>
#else
#import "SEGHTTPBusinessManager.h"
#endif

#if __has_include(<Commom/SEGAlertView.h>)
#import <Commom/SEGAlertView.h>
#else
#import "SEGAlertView.h"
#endif

#if __has_include(<SEGPlatform/SEGBussinessConstHeader.h>)
#import <SEGPlatform/SEGBussinessConstHeader.h>
#else
#import "SEGBussinessConstHeader.h"
#endif

#import "SEGIntegralExchangeModel.h"

static id _analyseBehaviorManager;
static dispatch_once_t onceToken;

@implementation SEGAnalyseBehaviorManager
#pragma mark - SINGLE----BEGIN----
+(instancetype)sharedManager{
    dispatch_once(&onceToken, ^{
        _analyseBehaviorManager = [[self alloc] init];
    });
    return _analyseBehaviorManager;
}

+(void)destroyResource{
    onceToken = 0;
    _analyseBehaviorManager = nil;
}

#pragma mark - SINGLE----END----

#pragma mark - HTTP----BEGIN----
/**
 根据业务计算成长值，积分
 @author Sam
 */
-(void)requestIntegralGrowthWithCode:(NSString*)code finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished {
    [self requestIntegralGrowthWithCode:code module:nil businessID:nil finished:finished];
}

/// 根据业务计算成长值，积分
/// @param code 积分行为
/// @param module 所属模块
/// @param businessID 对应详情ID
/// @param finished finished
- (void)requestIntegralGrowthWithCode:(NSString*)code module:(NSString *)module businessID:(NSString *)businessID finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished {
    if (finished) finished(YES, nil, nil, nil);
}

/// 根据用户ID获取用户等级，成长值
-(void)requestIntegralLeveluserIds:(NSString*)userIds finished:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished
{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:userIds forKey:@"userIds"];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kIntegral_api,@"/integral/user/level.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:parmas completed:^(BOOL isSuccessed, id result, NSString *codeType, NSError *error) {
        {
            if (!isSuccessed) {
                finished(NO, result ,codeType, error.localizedDescription);
                return ;
            }
            
            NSDictionary* data = [result valueForKey:@"data"];
            NSArray* levels = [data valueForKey:@"levels"];
            if (!levels || ![levels isKindOfClass:[NSArray class]]) {
                finished(NO, result ,codeType, error.localizedDescription);
            }
            
            NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:levels.count];
            for (NSDictionary* dic in levels) {
                SEGLevelDeatailModel* _model = [SEGLevelDeatailModel yy_modelWithDictionary:dic];
                [arrM addObject:_model];
            }
            
            finished(YES, arrM, codeType, error.localizedDescription);
        }
    }];
}


/// 积分抢兑的接口
- (void)requestIntegralEexchangeDataWithFinish:(void(^)(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage))finished {
    //http://gamma.uhomecp.com/integral-api/integral/ordinaryCoin/notice?pageSize=15&pageNo=1
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kIntegral_api,@"/integral/ordinaryCoin/notice",nil];
    NSDictionary *parameters = @{
        @"pageSize":@(30),
        @"pageNo":@(1)
    };
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:parameters finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            if ([result isKindOfClass:[NSDictionary class]] && [result[@"data"] isKindOfClass:[NSDictionary class]] && [result[@"data"][@"notices"] isKindOfClass:[NSArray class]]) {
                
                NSMutableArray *models = [NSMutableArray array];
                NSArray *data = result[@"data"][@"notices"];
                [data enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    SEGIntegralExchangeModel *model = [SEGIntegralExchangeModel yy_modelWithDictionary:obj];
                    [models addObject:model];
                }];
                
                finished(isSuccessed, models, codeType, errorMessage);
                return;
            }
        }
        finished(NO, result, codeType, errorMessage);
    }];
}

#pragma mark - HTTP----END----

#pragma mark - GETTER----BEGIN----
#pragma mark - GETTER----END----
@end
